package com.link.base.base.product.controller;

import com.link.base.base.product.model.ProdColl;
import com.link.base.base.product.model.ProdCollCount;
import com.link.base.base.product.service.ProdCollService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 产品收藏
 */
@Controller
@RequestMapping("/link/prodColl")
public class ProdCollController extends BasicController<ProdColl> {

    @Resource
    private ProdCollService prodCollService;

    @Override
    public BasicService<ProdColl> getBasicService() throws Exception {
        return prodCollService;
    }

    @Override
    public void beforQueryAllOrExample(ProdColl entity, HttpServletRequest request) throws Exception {
        super.beforQueryAllOrExample(entity, request);
        entity.setEmpId(UserUtil.getUser().getId());
        if (StringUtils.isNotBlank(entity.getAttr4())) {
            //多标签筛选
            List<String> tagIdList = new ArrayList<String>();
            String[] tagIds = entity.getAttr4().split(",");
            for (String tagid : tagIds) {
                tagIdList.add(tagid);
            }
            entity.setIds(tagIdList);
        }
    }

    @Override
    public void beforInsert(ProdColl entity, HttpServletRequest request) throws Exception {
        super.beforInsert(entity, request);
        CoreUser user = UserUtil.getUser();
        if (entity.getProdId() == null) {
            throw new ServiceException("ORDER-085");
        }
        if (user.getAcctId() == null) {
            throw new ServiceException("ACCT-014");
        }
        if (BasicService.MP.equals(entity.getLoginType())) {
            //存储消费者id
            entity.setAcctId(user.getId());
        } else {
            entity.setEmpId(user.getId());
        }
        //是否有效
        entity.setActiveFlag("Y");
    }

    @Override
    public void beforDelete(ProdColl entity, HttpServletRequest request) throws Exception {
        super.beforDelete(entity, request);
        ProdColl vaildEntity = prodCollService.queryById(entity);
        if (vaildEntity == null) {
            throw new ServiceException("PRODUCT-030");
        }
        CoreUser user = UserUtil.getUser();
        //登陆类型为小程序
        if (BasicService.MP.equals(entity.getLoginType())) {
            if (!user.getId().equals(vaildEntity.getAcctId())) {
                throw new ServiceException("PUBLIC-031");
            }
        } else if (!user.getId().equals(vaildEntity.getEmpId())) {
            throw new ServiceException("PUBLIC-031");
        }
    }

    /**
     * 根据产品ID删除一条记录
     */
    @RequestMapping(value = "/deleteByProdId")
    @ResponseBody
    public Map<String, Object> deleteByProdId(@JsonParam ProdColl entity, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            prodCollService.deleteByProdId(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 根据用户Id和产品Id删除一条记录
     */
    @RequestMapping(value = "/deleteByUPIds")
    @ResponseBody
    public Map<String, Object> deleteByUPIds(@RequestBody ProdColl entity, HttpSession session, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            prodCollService.deleteByUPIds(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取收藏夹数量类表
     */
    @RequestMapping(value = "/countProduct")
    @ResponseBody
    public Map<String, Object> countProduct(ProdColl entity, HttpSession session, HttpServletRequest request,
                                            HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            int currentCount = entity.getCurrentCount();
            entity.setStartRow(currentCount + 1);
            entity.setEndRow(currentCount + entity.getRows());
            List<ProdCollCount> list = prodCollService.countProduct(entity);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    /**
     * 获取收藏夹数量类表
     */
    @RequestMapping(value = "/countProductByEmpId")
    @ResponseBody
    public Map<String, Object> countProductByEmpId(ProdColl entity, HttpSession session, HttpServletRequest request,
                                                   HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ProdCollCount collCount = prodCollService.countProductByEmpId(entity);
            result.put("success", true);
            result.put("result", collCount);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    /**
     * 获取收藏夹数量类表 lujiang
     */
    @RequestMapping(value = "/queryByCollExamplePage")
    @ResponseBody
    public Map<String, Object> queryByCollExamplePage(@JsonParam QueryParams qps,@JsonParam ProdColl entity,
                                                      HttpSession session, HttpServletRequest request,
                                                      HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            qps.preDealSecurity(request);
            ProdColl temp = (ProdColl)BasicModel.transformClass(entity, qps);
            this.beforQueryAllOrExample(temp, request);
            this.beforQueryExample(temp, request);
            List<ProdColl> list = prodCollService.queryByCollExamplePage(entity);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
